package com.nbsaas.boot.product.data.repository;

import com.nbsaas.boot.product.data.entity.ComponentData;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.jpa.repository.support.JpaRepositoryImplementation;
import org.springframework.data.repository.query.Param;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
* 组件数据-数据处理功能
*/
public interface ComponentDataRepository extends JpaRepositoryImplementation<ComponentData, Serializable> {

    /**
     * 根据组件ID查找数据
     */
    List<ComponentData> findByComponentIdOrderBySort(Long componentId);

    /**
     * 根据数据类型查找
     */
    List<ComponentData> findByDataType(String dataType);

    /**
     * 根据是否显示查找
     */
    List<ComponentData> findByVisible(Boolean visible);

    /**
     * 根据组件ID和数据类型查找
     */
    @Query("SELECT cd FROM ComponentData cd WHERE cd.componentId = :componentId AND cd.dataType = :dataType ORDER BY cd.sort")
    List<ComponentData> findByComponentIdAndDataType(@Param("componentId") Long componentId, @Param("dataType") String dataType);

    /**
     * 查找可见的数据
     */
    @Query("SELECT cd FROM ComponentData cd WHERE cd.visible = true ORDER BY cd.sort")
    List<ComponentData> findVisibleData();

    /**
     * 查找有效时间内的数据
     */
    @Query("SELECT cd FROM ComponentData cd WHERE cd.visible = true AND (cd.startTime IS NULL OR cd.startTime <= :now) AND (cd.endTime IS NULL OR cd.endTime >= :now) ORDER BY cd.sort")
    List<ComponentData> findValidData(@Param("now") Date now);

    /**
     * 根据目标对象ID查找
     */
    List<ComponentData> findByTargetId(Long targetId);
} 